/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.service.ro.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.us.usws.biz.ResourceURIReplacer;
import com.us.usws.common.SiteConstants;
import com.us.usws.dao.UserDAO;
import com.us.usws.dao.UserEventDAO;
import com.us.usws.dao.VideoDAO;
import com.us.usws.dao.VideoFileDAO;
import com.us.usws.model.domain.userevent.EventFavVideo;
import com.us.usws.model.domain.userevent.EventShareVideo;
import com.us.usws.model.domain.userevent.UserEvent;
import com.us.usws.model.domain.video.Video;
import com.us.usws.model.domain.video.VideoFile;
import com.us.usws.model.flat.system.DOUser;
import com.us.usws.service.ro.UserEventROService;

/**
 * Service RO impl class
 * @author robot
 */
public class UserEventROServiceImpl implements UserEventROService {

	private static Log log = LogFactory.getFactory().getInstance(UserEventROServiceImpl.class);

	/**
	 * DAO instance
	 */
	private UserEventDAO dao = new UserEventDAO();
	private VideoDAO vDao = new VideoDAO();
	private VideoFileDAO vfDao = new VideoFileDAO();
	private UserDAO userDao = new UserDAO();

	/**
	 * Get Count.
	 */
	@Override
	public int getTotalCount() {
		return dao.getTotalCount();
	}

	/**
	 * Get all list.
	 */
	@Override
	public List<UserEvent> getAllUserEvents() {
		return dao.getAllUserEvents();
	}

	/**
	 * Get all list by pagination.
	 */
	@Override
	public List<UserEvent> getUserEvents(int start, int end) {
		return dao.getUserEvents(start, end);
	}

	/**
	 * Get UserEvent by primary key.
	 */
	@Override
	public UserEvent getUserEvent(String uid) {
		UserEvent u = dao.getUserEvent(uid);
		return u;
	}

	@Override
	public int getUserEventCountByUser(String userUid, int optrType) {
		return dao.getUserEventCountByUser(userUid, optrType);
	}

	@Override
	public List<UserEvent> getUserEventsByUser(String userUid, int optrType, int start, int pageSize) {
		return dao.getUserEventsByUser(userUid, optrType, start, pageSize);
	}

	@Override
	public int getFavVideoCount(String userUid) {
		return dao.getUserEventCountByUser(userUid, SiteConstants.DSID_USER_EVENT_OPTR_TYPE_ADD_FAV);
	}

	@Override
	public List<EventFavVideo> getFavVideos(String userUid, int start, int pageSize) {
		List<UserEvent> events = getUserEventsByUser(userUid, SiteConstants.DSID_USER_EVENT_OPTR_TYPE_ADD_FAV, start, pageSize);
		if (events == null) {
			return null;
		}

		List<EventFavVideo> ret = new ArrayList<EventFavVideo>(events.size());
		for (UserEvent each : events) {
			EventFavVideo one = new EventFavVideo();
			one.setDescription(each.getDescription());
			one.setUid(each.getUid());
			one.setUserUid(each.getUserUid());

			//video uid
			String videoUid = each.getEventId();
			Video v = vDao.getVideo(videoUid);
			if (v == null) {
				log.warn("Taret fav video's gone! videoUid=[" + each.getEventId() + "].");
				continue;
			}
			VideoFile vf = vfDao.getVideoFile(v.getFileUid());

			one.setVideoCreateByUid(v.getCreateByUid());
			one.setVideoCreateDate(v.getCreateDate());
			one.setVideoName(v.getName());
			one.setVideoUid(videoUid);

			//snapshot
			String snapshot = ResourceURIReplacer.getVideoSnapshotURL(vf.getUid(), vf.getSnapshot());
			one.setVideoSnapshot(snapshot);

			one.setVideoUrl(ResourceURIReplacer.getVideoFileURL(vf.getUid(), vf.getFileExt()));

			//user
			DOUser u = userDao.getDOUser(v.getCreateByUid());
			one.setVideoCreateByName(u.getFullName());

			//add to ret
			ret.add(one);
		}
		return ret;
	}

	@Override
	public int getShareVideoCount(String userUid) {
		return dao.getUserEventCountByUser(userUid, SiteConstants.DSID_USER_EVENT_OPTR_TYPE_SHARE);

	}

	@Override
	public List<EventShareVideo> getShareVideos(String userUid, int start, int pageSize) {
		List<UserEvent> events = getUserEventsByUser(userUid, SiteConstants.DSID_USER_EVENT_OPTR_TYPE_SHARE, start, pageSize);
		if (events == null) {
			return null;
		}

		List<EventShareVideo> ret = new ArrayList<EventShareVideo>(events.size());
		for (UserEvent each : events) {
			EventShareVideo one = new EventShareVideo();
			one.setDescription(each.getDescription());
			one.setUid(each.getUid());
			one.setUserUid(each.getUserUid());
			one.setShareViewLevel(each.getViewLevel());
			one.setShareViewTarget(each.getViewTarget());

			//video uid
			String videoUid = each.getEventId();
			Video v = vDao.getVideo(videoUid);
			if (v == null) {
				log.warn("Taret shared video's gone! videoUid=[" + each.getEventId() + "].");
				continue;
			}
			VideoFile vf = vfDao.getVideoFile(v.getFileUid());

			one.setVideoCreateDate(v.getCreateDate());
			one.setVideoName(v.getName());
			one.setVideoUid(videoUid);

			//snapshot
			String snapshot = ResourceURIReplacer.getVideoSnapshotURL(vf.getUid(), vf.getSnapshot());
			one.setVideoSnapshot(snapshot);

			one.setVideoUrl(ResourceURIReplacer.getVideoFileURL(vf.getUid(), vf.getFileExt()));

			//user
			DOUser u = userDao.getDOUser(v.getCreateByUid());
			one.setUserName(u.getFullName());

			//add to ret
			ret.add(one);
		}
		return ret;
	}
}